package copyengine.ui.component.progress.animation
{
    import com.greensock.TweenMax;

    import copyengine.utils.CEConst;

    import starling.display.Canvas;
    import starling.display.DisplayObject;

    public class CEMaskProgressBarAnimation implements ICEProgressBarAnimation
    {
        private var mTarget:DisplayObject;
        private var mDirection:int;
        private var mMask:Canvas;
        private var mCurrentTween:TweenMax;

        public function CEMaskProgressBarAnimation() {
        }

        public function initialize(_target:DisplayObject, _direction:int):void {
            mTarget = _target;
            mDirection = _direction;
            mMask = new Canvas();
        }

        public function onPercentChange(_nowPercent:Number):void {
            if (mCurrentTween != null) {
                mCurrentTween.kill();
                mCurrentTween = null;
            }
            mCurrentTween = TweenMax.to(this, 0.5, {tweenPercent: _nowPercent, onComplete: onTweenComplete})
        }

        private var mTweenPercent:Number = 100;

        public function set tweenPercent(_percent:Number):void {
            mTweenPercent = _percent;
            mMask.clear();
            if (mDirection == CEConst.DIR_HORIZONTAL) {
                mMask.drawRectangle(0, 0, mTarget.width * mTweenPercent * 0.01, mTarget.height);
            } else {
                mMask.drawRectangle(0, 0, mTarget.width, mTarget.height * mTweenPercent * 0.01);
            }
            mTarget.mask = mMask;
        }

        public function get tweenPercent():Number {return mTweenPercent;}

        private function onTweenComplete():void {
            mCurrentTween = null;
        }

    }
}
